var batchMap = new Object();
var batchIdx = 0;
$._ame_ = {
  ping: function () {
    return 'success'
  },
  renderFromBridge: function (talk, params) {
    var frontend = app.getFrontend();
    var wrapper = frontend.addDLToBatch(params.projectPath, params.format, params.preset, params.sequenceId, params.outputPath);

    wrapper.loadFormat(params.format);
    wrapper.loadPreset(params.preset);

    wrapper.onEncodeProgress = function (result) {
      var info = new Object()
      info.id = params.id
      info.progress = result
      var payload = new Object()
      payload.target = talk.sender
      payload.type = 'com.sobey.adobe.event.render-progress'
      payload.info = info
      $._.talkTo(payload)
    };
    wrapper.onEncodeFinished = function (result) {
      var info = new Object()
      info.id = params.id
      info.status = result
      info.filePath = wrapper.outputFiles[0]
      info.msg = wrapper.getLogOutput()
      var payload = new Object()
      payload.target = talk.sender
      payload.type = 'com.sobey.adobe.event.render-complete'
      payload.info = info
      $._.talkTo(payload)
    };
    var exh = app.getEncoderHost();
    exh.runBatch();
  },
  getFormats: function () {
    var encoderHost = app.getEncoderHost();
    var formatList = encoderHost.getFormatList();
    var formatPresetList = [];
    for (var i = 0; i < formatList.length; i++) {
      var format = formatList[i];
      var encoderWrapper = encoderHost.createEncoderForFormat(format);
      var presets = encoderWrapper.getPresetList();
      formatPresetList.push({ format: format, presets: presets.length ? presets : void 0 });
    }
    return formatPresetList
  },
  removeAllBatchItems: function () {
    return app.getExporter().removeAllBatchItems()
  },
  getDLItemsAtRoot: function (projectPath) {
    return app.getFrontend().getDLItemsAtRoot(projectPath)
  },
  processBatchItem: function (batchItem) {
    batchIdx++
    batchItem._id = batchIdx
    batchMap[batchIdx] = batchItem
    batchItem.onEncodeFinished = function (result) {
      $._.emit('com.sobey.adobe.event.encode-finished', {
        output: batchItem.outputFiles,
        status: batchItem.status,
        data: result,
        id: batchItem._id,
        type: 'finish'
      })
    }
    batchItem.onEncodeProgress = function (result) {
      $._.emit('com.sobey.adobe.event.encode-progress', {
        status: batchItem.status,
        data: result,
        id: batchItem._id,
        type: 'progress'
      })
    }
    batchItem.onEncodeFailed = function (result) {
      $._.emit('com.sobey.adobe.event.encode-failed', {
        status: batchItem.status,
        data: result,
        id: batchItem._id,
        type: 'fail'
      })
    }
    return batchIdx
  },
  addDLToBatch: function (project, format, preset, guid, destination) {
    try {
      console.log('addDLToBatch params:', decodeURIComponent(project), decodeURIComponent(format), preset, guid, decodeURIComponent(destination))
      var batchItem = app.getFrontend().addDLToBatch(decodeURIComponent(project), decodeURIComponent(format), preset, guid, decodeURIComponent(destination))
      batchItem.setWorkArea(1, 1, 1);
      var id = $._ame_.processBatchItem(batchItem)
      return id
    } catch (e) {
      console.log(JSON.stringify(e))
      return e
    }
  },
  addCompToBatch: function (project, preset, destination) {
    var batchItem = app.getFrontend().addCompToBatch(decodeURIComponent(project), decodeURIComponent(preset), decodeURIComponent(destination))
    var id = $._pr_.processBatchItem(batchItem)
    return id
  },
  addItemToBatch: function () {
    var batchItem = app.getFrontend().addItemToBatch(decodeURIComponent(file))
    var id = $._pr_.processBatchItem(batchItem)
    return id
  },
  addFileToBatch: function (file, format, preset, destination) {
    var batchItem = app.getFrontend().addFileToBatch(decodeURIComponent(file), format, decodeURIComponent(preset), decodeURIComponent(destination))
    var id = $._pr_.processBatchItem(batchItem)
    return id
  },
  addXMLToBatch: function () {
    var batchItem = app.getFrontend().addXMLToBatch()
    var id = $._pr_.processBatchItem(batchItem)
    return id
  },
  runBatch: function () {
    return app.getEncoderHost().runBatch()
  },
  stopBatch: function () {
    return app.getEncoderHost().stopBatch()
  },
  pauseBatch: function () {
    return app.getEncoderHost().pauseBatch()
  },
  isBatchRunning: function () {
    return app.getEncoderHost().isBatchRunning()
  },
  getFormatList: function () {
    return app.getEncoderHost().getFormatList()
  }
}

BridgeTalk.onReceive = function (talk) {
  try {
    var body = JSON.parse(talk.body)
    body.action && $._.talkTo({ target: talk.sender, type: body.type, info: $._ame_[body.action](talk, body.params) })
  }
  catch (e) { }

  try {
    if (!talk.type) return;
    $._.emit(talk.type, JSON.parse(talk.headers.info));
  }
  catch (e) { }
};